<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是STM32开发——串口通信基础知识的一些相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV16-15-串口通信-02-串口接收发送实现">
<meta property="og:url" content="https://sumumm.github.io/post/f38b79ed.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是STM32开发——串口通信基础知识的一些相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502122716375.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502123025937.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502123141432.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502123441664.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502134731398.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502143700118.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502143746779.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502163258549.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502164936890.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502171406639.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502171654794.png">
<meta property="article:published_time" content="2023-06-04T14:07:01.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.010Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV16-STM32开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502122716375.png">


<link rel="canonical" href="https://sumumm.github.io/post/f38b79ed.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/f38b79ed.html","path":"post/f38b79ed.html","title":"LV16-15-串口通信-02-串口接收发送实现"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV16-15-串口通信-02-串口接收发送实现 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E4%B8%B2%E5%8F%A3%E6%89%93%E5%8D%B0%E5%AE%9E%E7%8E%B0"><span class="nav-text">一、串口打印实现</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%A1%AC%E4%BB%B6%E8%AE%BE%E8%AE%A1"><span class="nav-text">1. 硬件设计</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-STM32CubeMX%E9%85%8D%E7%BD%AE"><span class="nav-text">2. STM32CubeMX配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%87%BD%E6%95%B0%E5%AE%9E%E7%8E%B0"><span class="nav-text">3. 函数实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-HAL%E5%BA%93%E5%87%BD%E6%95%B0%E6%94%B6%E5%8F%91%E6%95%B0%E6%8D%AE"><span class="nav-text">3.1 HAL库函数收发数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E5%8F%91%E9%80%81%E4%B8%80%E4%B8%AA%E5%AD%97%E8%8A%82%E6%95%B0%E6%8D%AE"><span class="nav-text">3.2 发送一个字节数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E6%8E%A5%E6%94%B6%E4%B8%80%E4%B8%AA%E5%AD%97%E8%8A%82%E6%95%B0%E6%8D%AE"><span class="nav-text">3.3 接收一个字节数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-printf%E5%AE%9E%E7%8E%B0"><span class="nav-text">3.4 printf实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-printf%E7%9A%84%E4%B8%80%E4%B8%AA%E5%9D%91"><span class="nav-text">4. printf的一个坑</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E4%BD%BF%E7%94%A8%E5%BE%AE%E5%BA%93"><span class="nav-text">4.1 使用微库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E9%81%BF%E5%85%8D%E4%BD%BF%E7%94%A8%E5%8D%8A%E4%B8%BB%E6%9C%BA%E6%A8%A1%E5%BC%8F"><span class="nav-text">4.2 避免使用半主机模式</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-%E5%8D%8A%E4%B8%BB%E6%9C%BA%E6%A8%A1%E5%BC%8F"><span class="nav-text">4.2.1 半主机模式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-2-%E5%85%B3%E9%97%AD%E5%8D%8A%E4%B8%BB%E6%9C%BA%E6%A8%A1%E5%BC%8F"><span class="nav-text">4.2.2 关闭半主机模式</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%BC%80%E5%90%AF%E4%B8%B2%E5%8F%A3%E4%B8%AD%E6%96%AD"><span class="nav-text">二、开启串口中断</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%B8%80%E8%88%AC%E6%AD%A5%E9%AA%A4"><span class="nav-text">1. 一般步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-STM32CubeMX%E4%B8%AD%E6%96%AD%E9%85%8D%E7%BD%AE"><span class="nav-text">2. STM32CubeMX中断配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%BC%96%E5%86%99%E4%B8%AD%E6%96%AD%E6%9C%8D%E5%8A%A1%E5%87%BD%E6%95%B0"><span class="nav-text">3. 编写中断服务函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%B8%B2%E5%8F%A3%E4%B8%AD%E6%96%AD%E4%B8%AD%E7%9A%84%E4%B8%80%E4%B8%AA%E5%9D%91"><span class="nav-text">4. 串口中断中的一个坑</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C"><span class="nav-text">5. 运行结果</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E8%AE%BE%E8%AE%A1"><span class="nav-text">三、串口接收设计</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%9B%B8%E5%85%B3%E5%AE%9A%E4%B9%89"><span class="nav-text">1. 相关定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%8E%A5%E6%94%B6%E4%B8%AD%E6%96%AD%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0"><span class="nav-text">2. 接收中断回调函数</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81printf%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93"><span class="nav-text">四、printf问题总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-MDK%E4%B8%AD%E6%97%A0%E6%B3%95%E6%89%93%E5%8D%B0%E5%A4%84%E7%90%86"><span class="nav-text">1. MDK中无法打印处理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-STM32CubeIDE%E4%B8%AD%E6%97%A0%E6%B3%95%E6%89%93%E5%8D%B0%E5%A4%84%E7%90%86"><span class="nav-text">2. STM32CubeIDE中无法打印处理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-Makefile%E5%B7%A5%E7%A8%8B%E6%97%A0%E6%B3%95%E6%89%93%E5%8D%B0%E5%A4%84%E7%90%86"><span class="nav-text">3. Makefile工程无法打印处理</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/f38b79ed.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV16-15-串口通信-02-串口接收发送实现 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV16-15-串口通信-02-串口接收发送实现
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-06-04 22:07:01" itemprop="dateCreated datePublished" datetime="2023-06-04T22:07:01+08:00">2023-06-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV16-STM32开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>25 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是STM32开发——串口通信基础知识的一些相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <ul><li>通用</li></ul><table><tr><td align="center">分类  </td><td align="center">网址</td><td align="center">说明</td></tr><tr><td align="center" rowspan="4">官方网站</td><td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td><td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td></tr><tr>                                            <td align="left"><a href="https://www.st.com/content/st_com/zh.html" target="_blank">https://www.st.com/content/st_com/zh.html</a></td><td align="left">ST官方网站，在这里我们可以找到STM32的相关文档</td></tr><tr>                                            <td align="left"><a href="https://www.stmcu.com.cn/" target="_blank">https://www.stmcu.com.cn/</a></td><td align="left">意法半导体ST中文官方网站，在这里我们可以找到STM32的相关中文参考文档</td></tr><tr>                                            <td align="left"><a href="http://elm-chan.org/fsw/ff/00index_e.html" target="_blank">http://elm-chan.org/fsw/ff/00index_e.html</a></td><td align="left">FatFs文件系统官网</td></tr><tr><td align="center" rowspan="3">教程书籍</td><td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M3权威指南(中文).pdf" target="_blank">《ARM Cortex-M3权威指南》</a></td><td alirn="left" rowspan="3">ARM公司专家Joseph Yiu（姚文祥）的力作，中文翻译是NXP的宋岩</td></tr><tr>                                            <td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M0权威指南(中文).pdf" target="_blank">《ARM Cortex-M0权威指南》</a></td></tr><tr>                                            <td align="left"><a href="" target="_blank">《ARM Cortex-M3与Cortex-M4权威指南》</a></td></tr><tr><td align="center" rowspan="4">开发论坛</td><td align="left"><a href="http://47.111.11.73/forum.php" target="_blank">http://47.111.11.73/forum.php</a></td><td align="left">开源电子网，正点原子的资料下载及问题讨论论坛</td></tr><tr>                                            <td align="left"><a href="https://www.firebbs.cn/forum.php" target="_blank">https://www.firebbs.cn/forum.php</a></td><td align="left">国内Kinetis开发板-野火/秉火（刘火良）主持的论坛，现也做STM32和i.MX RT</td></tr><tr>                                            <td align="left"><a href="https://www.amobbs.com/index.php" target="_blank">https://www.amobbs.com/index.php</a></td><td align="left">阿莫（莫进明）主持的论坛，号称国内最早最火的电子论坛，以交流Atmel AVR系列单片机起家，现已拓展到嵌入式全平台，其STM32系列帖子有70W+。</td></tr><tr>                                            <td align="left"><a href="http://download.100ask.net/index.html" target="_blank">http://download.100ask.net/index.html</a></td><td align="left">韦东山嵌入式资料中心，有些STM32和linux的相关资料也可以来这里找。</td></tr><tr><td align="center" rowspan="3">博客参考</td><td align="left"><a href="http://www.openedv.com/" target="_blank">http://www.openedv.com/</a></td><td align="left">开源网-原子哥个人博客</td></tr><tr>                                            <td align="left"><a href="http://blog.chinaaet.com/jihceng0622" target="_blank">http://blog.chinaaet.com/jihceng0622</a></td><td align="left">博主是原Freescale现NXP的现场应用工程师</td></tr><tr>                                            <td align="left"><a href="https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/cortex-m-resources" target="_blank">cortex-m-resources</a></td><td align="left">这其实并不算是一个博客，这是ARM公司专家Joseph Yiu收集整理的所有对开发者有用的官方Cortex-M资料链接（也包含极少数外部资源链接）</td></tr></table><ul><li>STM32</li></ul><table>    <tr><td align="center" rowspan="2">STM32</td><td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103/hal_general/zh/latest/index.html" target="_blank">STM32 HAL库开发实战指南——基于F103系列开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr><tr>                                            <td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103badao/std/zh/latest/index.html" target="_blank">STM32库开发实战指南——基于野火霸道开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr></table><ul><li>SD卡</li></ul><table>    <tr><td align="left"><a href="https://www.sdcard.org/" target="_blank">SD Association</a></td><td align="left">提供了SD存储卡和SDIO卡系统规范</td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="left"><a href="https://www.st.com/resource/en/datasheet/stm32f103ze.pdf" target="_blank">STM32F103xx英文数据手册</a></td><td align="left">STM32F103xC/D/E系列的英文数据手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/709978" target="_blank">STM32F103xx中文数据手册</a></td><td align="left">STM32F103xC/D/E系列的中文数据手册</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf" target="_blank">STM32F10xxx英文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的英文参考手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/710001" target="_blank">STM32F10xxx中文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的中文参考手册</td></tr>    <tr><td align="left"><a href="https://developer.arm.com/documentation/100165/0201/?lang=en" target="_blank">Arm Cortex-M3 处理器技术参考手册-英文版</a></td><td align="left">Cortex-M3技术参考手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf" target="_blank">STM32F10xxx Cortex-M3编程手册-英文版(PM0056)</a></td><td align="left">STM32F10xxx/20xxx/21xxx/L1xxxx系列Cortex-M3编程手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/" target="_blank">SD卡相关资料——最新版本</a></td><td align="left">有关SD卡的一些资料可以从这里下载</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/archives/" target="_blank">SD卡相关资料——历史版本</a></td><td align="left">有关SD卡的一些历史版本资料可以从这里下载，比如后边看的SD卡2.0协议</td></tr>    <tr><td align="left"><a href="./" target="_blank">SD 2.0 协议标准完整版</a></td><td align="left">这是一篇关于SD卡2.0协议的中文文档，还是比较有参考价值的，可以一看</td></tr></table>
              </div>
            </details>

<h1 id="一、串口打印实现"><a href="#一、串口打印实现" class="headerlink" title="一、串口打印实现"></a><font size=3>一、串口打印实现</font></h1><p>说明，这里使用STM32CubeM进行配置和导出工程。</p>
<h2 id="1-硬件设计"><a href="#1-硬件设计" class="headerlink" title="1. 硬件设计"></a><font size=3>1. 硬件设计</font></h2><p>我使用的是正点原子的战舰V3，串口部分电路如下，串口 1 与 USB 串口并没有在 PCB 上连接在一起，需要通过跳线帽来连接一下。这里我们把 P4 的 RXD 和 TXD 用跳线帽与 PA9 和 PA10 连接起来。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502122716375.png" alt="image-20230502122716375" style="zoom: 67%;" />

<h2 id="2-STM32CubeMX配置"><a href="#2-STM32CubeMX配置" class="headerlink" title="2. STM32CubeMX配置"></a><font size=3>2. STM32CubeMX配置</font></h2><p>注意这里的时钟都没配置，会默认开启的，这一部分不涉及中断相关内容。</p>
<ul>
<li>（1）PA9配置（TXD）</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502123025937.png" alt="image-20230502123025937" style="zoom: 80%;" />

<ul>
<li>（2）PA10配置（RXD）</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502123141432.png" alt="image-20230502123141432" style="zoom:85%;" />

<ul>
<li>（3）USART1配置</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502123441664.png" alt="image-20230502123441664" style="zoom: 33%;" />

<ul>
<li>（4）导出工程即可</li>
</ul>
<h2 id="3-函数实现"><a href="#3-函数实现" class="headerlink" title="3. 函数实现"></a><font size=3>3. 函数实现</font></h2><p>上边只是完成了基础的初始化等功能，具体的接收，发送，库里边有对应的函数，我们自己写一下熟悉一下过程。</p>
<h3 id="3-1-HAL库函数收发数据"><a href="#3-1-HAL库函数收发数据" class="headerlink" title="3.1 HAL库函数收发数据"></a><font size=3>3.1 HAL库函数收发数据</font></h3><p>HAL库提供了两个函数，让我们可以获取和发送数据，它们定义在 stm32f1xx_hal_uart.c： </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">HAL_StatusTypeDef <span class="title function_">HAL_UART_Transmit</span><span class="params">(UART_HandleTypeDef *huart, <span class="type">uint8_t</span> *pData, <span class="type">uint16_t</span> Size, <span class="type">uint32_t</span> Timeout)</span>;</span><br><span class="line">HAL_StatusTypeDef <span class="title function_">HAL_UART_Receive</span><span class="params">(UART_HandleTypeDef *huart, <span class="type">uint8_t</span> *pData, <span class="type">uint16_t</span> Size, <span class="type">uint32_t</span> Timeout)</span>;</span><br></pre></td></tr></table></figure>

<h3 id="3-2-发送一个字节数据"><a href="#3-2-发送一个字节数据" class="headerlink" title="3.2 发送一个字节数据"></a><font size=3>3.2 发送一个字节数据</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">Uart_Putchar</span><span class="params">(<span class="type">uint8_t</span> ch)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span>( !(USART1-&gt;SR &amp; (<span class="number">1</span>&lt;&lt;<span class="number">6</span>)));  <span class="comment">//等待DR为空</span></span><br><span class="line">	</span><br><span class="line">	USART1-&gt;DR = ch;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-3-接收一个字节数据"><a href="#3-3-接收一个字节数据" class="headerlink" title="3.3 接收一个字节数据"></a><font size=3>3.3 接收一个字节数据</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">uint8_t</span> <span class="title function_">Uart_Getchar</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span>(!(USART1-&gt;SR &amp; (<span class="number">1</span>&lt;&lt;<span class="number">5</span>)));  <span class="comment">//等待RDR不为空，表示收到数据</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> USART1-&gt;DR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-4-printf实现"><a href="#3-4-printf实现" class="headerlink" title="3.4 printf实现"></a><font size=3>3.4 printf实现</font></h3><p>我们在编写C语言的时候，经常使用printf，还可以格式化打印，这是不是很方便呢？我们也可以在STM32中使用printf来调用串口，我们只需要重写下边这个函数就可以啦：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">fputc</span><span class="params">(<span class="type">int</span> ch, FILE *f)</span>;</span><br></pre></td></tr></table></figure>

<p>我们重新定义该函数如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">fputc</span><span class="params">(<span class="type">int</span> ch, FILE *f)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span>((USART1-&gt;SR &amp; (<span class="number">1</span>&lt;&lt;<span class="number">6</span>)) == <span class="number">0</span>);  <span class="comment">//等待DR为空,循环发送,直到发送完毕 </span></span><br><span class="line">	</span><br><span class="line">    USART1-&gt;DR = (<span class="type">uint8_t</span>) ch;</span><br><span class="line">    </span><br><span class="line">	<span class="keyword">return</span> ch;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-printf的一个坑"><a href="#4-printf的一个坑" class="headerlink" title="4. printf的一个坑"></a><font size=3>4. printf的一个坑</font></h2><p>当我们重写了fputc后，只要调用printf函数，程序直接卡死，有两种解决办法，第二种方法中说明了产生这个问题的原因。</p>
<h3 id="4-1-使用微库"><a href="#4-1-使用微库" class="headerlink" title="4.1 使用微库"></a><font size=3>4.1 使用微库</font></h3><p>在MDK5中点击【options for target】（常见的哪个魔术棒），在【Target】&rarr;【Use MicroLIB】勾选，使用微库。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502134731398.png" alt="image-20230502134731398" style="zoom:50%;" />

<h3 id="4-2-避免使用半主机模式"><a href="#4-2-避免使用半主机模式" class="headerlink" title="4.2 避免使用半主机模式"></a><font size=3>4.2 避免使用半主机模式</font></h3><h4 id="4-2-1-半主机模式"><a href="#4-2-1-半主机模式" class="headerlink" title="4.2.1 半主机模式"></a><font size=3>4.2.1 半主机模式</font></h4><p>半主机是用于 ARM 目标的一种机制，可将来自应用程序代码的输入&#x2F;输出请求传送至运行调试器的主机。 例如，使用此机制可以启用 C 库中的函数，如 printf() 和 scanf()，来使用主机的屏幕和键盘，而不是在目标系统上配备屏幕和键盘。这种机制很有用，因为开发时使用的硬件通常没有最终系统的所有输入和输出设备。 半主机可让主机来提供这些设备。<strong>简单的来说，半主机模式就是通过仿真器实现开发板在电脑上的输入和输出。和半主机模式功能相同的是ITM调试机制。</strong></p>
<p>在嵌入式的编程中我们要使用printf、fopen、fclose等函数的，但是因为嵌入式的程序中并没有对这些函数的底层实现，使得设备运行时会进入软件中断BAEB处，这时就需要__use_no_semihosting_swi 这个声明，使程序遇到这些文件操作函数时不停在此中断处。当目标板脱离仿真器（jlink&#x2F;ulink）单独运行时，<strong>不能使用半主机模式</strong>。否则进入软件中断BAEB处，无法再执行下去，这就导致了卡死问题</p>
<h4 id="4-2-2-关闭半主机模式"><a href="#4-2-2-关闭半主机模式" class="headerlink" title="4.2.2 关闭半主机模式"></a><font size=3>4.2.2 关闭半主机模式</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> import(__use_no_semihosting)</span></span><br></pre></td></tr></table></figure>

<p>这条语句可以关闭半主机模式，只需要在任意一个C文件中加入即可。还有在使用keil编程的过程中还会遇到以下错误：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">..\OBJ\USART.axf: Error: L6915E: Library reports error: __use_no_semihosting was requested, but _ttywrch was referenced</span><br></pre></td></tr></table></figure>

<p>说的大概的意思就是关掉了半主机模式，但是函数__ttywrch被引用了，这时要把函数重写一遍，当然出现其他的函数被要求的时候，可以参考上面的函数进行编写，只要放到任意一个.c源文件之中即可。所以我们可以在任意一个.c文件中添加如下内容：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//加入以下代码,支持printf函数,而不需要选择use MicroLIB	  </span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 1</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> import(__use_no_semihosting)             </span></span><br><span class="line"><span class="comment">//标准库需要的支持函数                 </span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">FILE</span> </span></span><br><span class="line"><span class="class">&#123;</span> </span><br><span class="line">	<span class="type">int</span> handle; </span><br><span class="line">&#125;; </span><br><span class="line"></span><br><span class="line">FILE __stdout;       </span><br><span class="line"><span class="comment">//定义_sys_exit()以避免使用半主机模式    </span></span><br><span class="line"><span class="type">void</span> _sys_exit(<span class="type">int</span> x) </span><br><span class="line">&#123; </span><br><span class="line">	x = x; </span><br><span class="line">&#125; </span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> </span></span><br></pre></td></tr></table></figure>

<h1 id="二、开启串口中断"><a href="#二、开启串口中断" class="headerlink" title="二、开启串口中断"></a><font size=3>二、开启串口中断</font></h1><p>前边我们已经知道如何收发数据了，下边我们来看一下串口的中断操作。</p>
<h2 id="1-一般步骤"><a href="#1-一般步骤" class="headerlink" title="1. 一般步骤"></a><font size=3>1. 一般步骤</font></h2><p>（1）使能相应的时钟</p>
<p>（2）配置GPIO管脚为串口功能</p>
<p>（3）设置中断优先级</p>
<p>（4）使能相应的中断</p>
<p>（5）实现中断服务程序</p>
<h2 id="2-STM32CubeMX中断配置"><a href="#2-STM32CubeMX中断配置" class="headerlink" title="2. STM32CubeMX中断配置"></a><font size=3>2. STM32CubeMX中断配置</font></h2><p>对于GPIO等的配置，和前边保持一致，我们这里再加上中断的配置即可：</p>
<ul>
<li>NVIC配置</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502143700118.png" alt="image-20230502143700118" style="zoom:50%;" />

<ul>
<li>Code generation</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502143746779.png" alt="image-20230502143746779" style="zoom: 43%;" />

<h2 id="3-编写中断服务函数"><a href="#3-编写中断服务函数" class="headerlink" title="3. 编写中断服务函数"></a><font size=3>3. 编写中断服务函数</font></h2><p>我们找到最终的中断服务函数回调函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">USART1_IRQHandler()---&gt;HAL_UART_IRQHandler()---&gt;UART_Receive_IT()---&gt;HAL_UART_RxCpltCallback()</span><br></pre></td></tr></table></figure>

<p>我们只需要在 HAL_UART_RxCpltCallback() 编写我们的接收中断发生后要实现的功能即可：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">uint8_t</span> RX[<span class="number">10</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">HAL_UART_TxCpltCallback</span><span class="params">(UART_HandleTypeDef *huart)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(huart-&gt;Instance == USART1)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;uart tx end\r\n&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">HAL_UART_RxCpltCallback</span><span class="params">(UART_HandleTypeDef *huart)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(huart-&gt;Instance == USART1)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;REV : %x  %x\n&quot;</span>,RX[<span class="number">0</span>],RX[<span class="number">1</span>]);</span><br><span class="line">		HAL_UART_Receive_IT(&amp;huart1, RX, <span class="number">2</span>);<span class="comment">// 为什么要有设么一句？后边再说</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="4-串口中断中的一个坑"><a href="#4-串口中断中的一个坑" class="headerlink" title="4. 串口中断中的一个坑"></a><font size=3>4. 串口中断中的一个坑</font></h2><p>这坑出现在接收数据的时候，有这样一个函数，这个函数是在发生接收中断的时候获取接收到的数据：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">HAL_StatusTypeDef <span class="title function_">HAL_UART_Receive_IT</span><span class="params">(UART_HandleTypeDef *huart, <span class="type">uint8_t</span> *pData, <span class="type">uint16_t</span> Size)</span>;</span><br></pre></td></tr></table></figure>

<p>最开始，我以为这个函数仅仅就是单纯的接收数据用的，但是，坑来了，我配置完串口以及串口中断后，无法产生串口中断。我去检查了串口引脚、串口是否配置了异步模式、串口全局中断是否打开、NVIC是否进行了配置，结果发现这些配置都正确，但是就是不发生接收中断。查阅各种资料及例程后，发现了这个函数的含义并不是我所想象的那么简单，我们来看一下函数原型：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502163258549.png" alt="image-20230502163258549" style="zoom:80%;" />

<p>这个函数是以非阻塞模式接收一定数量的数据，第一个参数就是串口的句柄，我们使用的哪个串口就填那个串口的句柄地址，第二个参数是一个缓冲区，当中断发生的时候，接收到的数据会被存放在这里，第三个参数是大小，单位是字节，当我们填1的时候，接收到一个字节就会调用一次回调函数，当我们填10的时候，接收到的数据若不足10个字节，那么就不会调用回调函数，但是依然会产生接收中断。</p>
<p>我们可以试一下，若是我们程序从头到尾都没有调用这个函数的话，我们的接收中断是不会进入的，即便我们全局中断和串口中断都开起来了，我们仔细看上边的图，会发现内部调用了一个 UART_Start_Receive_IT () 函数，函数里边又调用了三个含有ENABLE_IT的函数，做的是不同的使能。</p>
<p>我们看注释，会发现，第三个在使能UART_IT_RXNE，我们来看一下他是啥，其实看注释也能知道，不过我们来追踪一下，印象深刻一些。我们可以定位到以下宏：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> UART_CR1_REG_INDEX               1U</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> USART_CR1_RXNEIE_Pos                (5U)                               </span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> USART_CR1_RXNEIE_Msk                (0x1UL &lt;&lt; USART_CR1_RXNEIE_Pos)     <span class="comment">/*!&lt; 0x00000020 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> USART_CR1_RXNEIE                    USART_CR1_RXNEIE_Msk               <span class="comment">/*!&lt; RXNE Interrupt Enable */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> UART_IT_RXNE                     ((uint32_t)(UART_CR1_REG_INDEX &lt;&lt; 28U | USART_CR1_RXNEIE))</span></span><br></pre></td></tr></table></figure>

<p>我们再来看一下CR1寄存器：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502164936890.png" alt="image-20230502164936890" style="zoom:50%;" />

<p>其中的 bit[5]：RXNEIE，接收缓冲区非空中断使能 (RXNE interrupt enable) ，该位由软件设置或清除。 0：禁止产生中断； 1：当USART_SR中的ORE或者RXNE为’1’时，产生USART中断。</p>
<p>到这里就清晰了，这个函数不仅仅会获取数据，还会开启接收中断，为什么这里要开启？我们再来往前看，我们看一下这个函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> HAL_StatusTypeDef <span class="title function_">UART_Receive_IT</span><span class="params">(UART_HandleTypeDef *huart)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 中间的省略...</span></span><br><span class="line">    <span class="keyword">if</span> (--huart-&gt;RxXferCount == <span class="number">0U</span>)</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="comment">/* Disable the UART Data Register not empty Interrupt */</span></span><br><span class="line">      __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);</span><br><span class="line">	  <span class="comment">// 中间的省略...</span></span><br><span class="line">      <span class="comment">/*Call legacy weak Rx complete callback*/</span></span><br><span class="line">      HAL_UART_RxCpltCallback(huart);</span><br><span class="line">	 <span class="comment">// 中间的省略...</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 后边的省略...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>额，中间太多没用的占行数，就都删了，大致有这个关系就行了，我们看一下这个的定义 RxXferCount ：UART Rx Transfer Counter翻译过来是 UART Rx传输计数器，我们看逻辑大概就是当这个值减到0的时候才会调用回调函数，这样不就联系起来了吗？我们通过 HAL_UART_Receive_IT 设置了串口的这个参数的值，然后每发生一次中断这个数就会自减1，当减到0的时候，调用回调函数，这是对于 HAL_UART_Receive_IT 第三个参数的理解，那么前边说的需要重新开启中断呢？我们看这个函数里，在 RxXferCount 减到0的时候，它先去关闭了接收中断，这样的话，每调用一次回调函数，就会关闭一次中断，我们要是不再开启接收中断的话，下一次接收就不会产生中断啦，这样就理清楚了这个关系了。</p>
<p><strong>【总结】</strong></p>
<p>（1）使用接收中断时，必须在初始化完成后调用一次 HAL_UART_Receive_IT() 函数来开启接收中断（其他地方实际上是没有开启接收中断的，这样才能产生第一次中断）</p>
<p>（2）每次调用回调函数的完成后一定要再调用一次 HAL_UART_Receive_IT() 来重新开启接收中断。</p>
<p>（3）HAL_UART_Receive_IT()只要调用，就会开启接收中断，但是中断多少次之后调用回调函数，由第三个参数决定，当第三个参数为1时，每接收到一个字节数据，都会调用一次回调函数，当不为1时，每接受一个字节都会产生一次中断，但是，接收到对应字节数的数据时才会调用一次回调函数。</p>
<h2 id="5-运行结果"><a href="#5-运行结果" class="headerlink" title="5. 运行结果"></a><font size=3>5. 运行结果</font></h2><p>我们将接收的字节数定义为 2，也就是接收到两个字节数据时才产生中断：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">HAL_UART_RxCpltCallback</span><span class="params">(UART_HandleTypeDef *huart)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(huart-&gt;Instance == USART1)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;receive: %x %x\r\n&quot;</span>, RXBUFF[<span class="number">0</span>], RXBUFF[<span class="number">1</span>]);</span><br><span class="line">		HAL_UART_Receive_IT(&amp;huart1, RXBUFF, <span class="number">2</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>接下来我们来看看效果：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502171406639.png" alt="image-20230502171406639" style="zoom:33%;" />

<p>我们发送一个2，会发现没有输出，我们再点一次发送，下方状态栏的S变为2，此时上方显示出我们发送出去的两个2，这也验证了我们之前的分析过程。有一点需要注意，在串口调试助手中，空格可能不被识别的，我们关注下方发送的字节数即可：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-15-%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1-02-%E4%B8%B2%E5%8F%A3%E6%8E%A5%E6%94%B6%E5%8F%91%E9%80%81%E5%AE%9E%E7%8E%B0/img/image-20230502171654794.png" alt="image-20230502171654794" style="zoom:33%;" />

<h1 id="三、串口接收设计"><a href="#三、串口接收设计" class="headerlink" title="三、串口接收设计"></a><font size=3>三、串口接收设计</font></h1><h2 id="1-相关定义"><a href="#1-相关定义" class="headerlink" title="1. 相关定义"></a><font size=3>1. 相关定义</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> RXBUFFERSIZE   1        <span class="comment">// 缓存大小，这个值决定了收到多少个字节数据执行一次回调函数</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> USART_REC_LEN  200      <span class="comment">// 定义最大接收字节数 200</span></span></span><br><span class="line"><span class="type">uint16_t</span> USART_RX_STA = <span class="number">0</span>;      <span class="comment">// 接收状态标记	  </span></span><br><span class="line">                                <span class="comment">// bit15 接收完成标志</span></span><br><span class="line">                                <span class="comment">// bit14 接收到0x0d</span></span><br><span class="line">                                <span class="comment">// bit13~0 接收到的有效字节数目</span></span><br><span class="line"><span class="type">uint8_t</span> aRxBuffer[RXBUFFERSIZE]; <span class="comment">// HAL库使用的串口接收缓冲</span></span><br><span class="line"><span class="comment">//注意,读取USARTx-&gt;SR能避免莫名其妙的错误   	</span></span><br><span class="line"><span class="type">uint8_t</span> USART_RX_BUF[USART_REC_LEN];     <span class="comment">//接收缓冲,最大USART_REC_LEN个字节.</span></span><br></pre></td></tr></table></figure>

<h2 id="2-接收中断回调函数"><a href="#2-接收中断回调函数" class="headerlink" title="2. 接收中断回调函数"></a><font size=3>2. 接收中断回调函数</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  串口接收中断回调函数</span></span><br><span class="line"><span class="comment">  * @note   </span></span><br><span class="line"><span class="comment">  * @param  huart 串口句柄</span></span><br><span class="line"><span class="comment">  * @retval </span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">HAL_UART_RxCpltCallback</span><span class="params">(UART_HandleTypeDef *huart)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="type">uint32_t</span> timeout = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">if</span> (huart-&gt;Instance == USART1) <span class="comment">// 如果是串口1</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="keyword">if</span> ((USART_RX_STA &amp; <span class="number">0x8000</span>) == <span class="number">0</span>) <span class="comment">// 接收未完成</span></span><br><span class="line">    &#123;</span><br><span class="line">      <span class="keyword">if</span> (USART_RX_STA &amp; <span class="number">0x4000</span>) <span class="comment">// 接收到了0x0d(回车)</span></span><br><span class="line">      &#123;</span><br><span class="line">        <span class="keyword">if</span> (aRxBuffer[<span class="number">0</span>] != <span class="number">0x0a</span>) <span class="comment">// 是否接收到了0x0a(换行)</span></span><br><span class="line">          USART_RX_STA = <span class="number">0</span>;       <span class="comment">// 接收错误,重新开始</span></span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">          USART_RX_STA |= <span class="number">0x8000</span>; <span class="comment">// 接收完成了</span></span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span> <span class="comment">// 还没收到0X0D</span></span><br><span class="line">      &#123;</span><br><span class="line">        <span class="keyword">if</span> (aRxBuffer[<span class="number">0</span>] == <span class="number">0x0d</span>)</span><br><span class="line">          USART_RX_STA |= <span class="number">0x4000</span>;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">          USART_RX_BUF[USART_RX_STA &amp; <span class="number">0X3FFF</span>] = aRxBuffer[<span class="number">0</span>];</span><br><span class="line">          USART_RX_STA++;</span><br><span class="line">          <span class="keyword">if</span> (USART_RX_STA &gt; (USART_REC_LEN - <span class="number">1</span>))</span><br><span class="line">            USART_RX_STA = <span class="number">0</span>; <span class="comment">// 接收数据错误,重新开始接收</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    timeout = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (HAL_UART_GetState(&amp;huart1) != HAL_UART_STATE_READY) <span class="comment">// 等待就绪</span></span><br><span class="line">    &#123;</span><br><span class="line">      timeout++; <span class="comment">////超时处理</span></span><br><span class="line">      <span class="keyword">if</span> (timeout &gt; HAL_MAX_DELAY)</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    timeout = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">// 一次处理完成之后，重新开启中断并设置RxXferCount为1</span></span><br><span class="line">    <span class="keyword">while</span> (HAL_UART_Receive_IT(&amp;huart1, (<span class="type">uint8_t</span> *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) </span><br><span class="line">    &#123;</span><br><span class="line">      timeout++; <span class="comment">// 超时处理</span></span><br><span class="line">      <span class="keyword">if</span> (timeout &gt; HAL_MAX_DELAY)</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>【注意】一定要注意回调函数执行完毕后，要重新调用一次HAL_UART_Receive_IT()函数。</p>
<h1 id="四、printf问题总结"><a href="#四、printf问题总结" class="headerlink" title="四、printf问题总结"></a><font size=3>四、printf问题总结</font></h1><p>前边我们在keil中编程的时候使用的是STM32CubeMX生成的工程，或者说自己使用标准库新建工程，出现重定向printf后无法打印的情况可以使用下边的处理方式1，但是后来我发现，在STM32CubeIDE中创建的工程，使用处理方式一后依然无法打印，后来上网搜了一下，又添加了一些东西，作为参考吧。</p>
<h2 id="1-MDK中无法打印处理"><a href="#1-MDK中无法打印处理" class="headerlink" title="1. MDK中无法打印处理"></a><font size=3>1. MDK中无法打印处理</font></h2><p>串口是我们经常要用到的，我们可以将fputc一起写到避免半主机模式的代码中，这样，后续我们只需要赋值这一段代码到工程就可以啦：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//加入以下代码,支持printf函数,而不需要选择use MicroLIB	  </span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 1</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> import(__use_no_semihosting)             </span></span><br><span class="line"><span class="comment">//标准库需要的支持函数                 </span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">FILE</span> </span></span><br><span class="line"><span class="class">&#123;</span> </span><br><span class="line">	<span class="type">int</span> handle; </span><br><span class="line">&#125;; </span><br><span class="line"></span><br><span class="line">FILE __stdin; </span><br><span class="line">FILE __stdout;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义_sys_exit()以避免使用半主机模式    </span></span><br><span class="line"><span class="type">void</span> _sys_exit(<span class="type">int</span> x) </span><br><span class="line">&#123; </span><br><span class="line">	x = x; </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//重定义fputc函数 </span></span><br><span class="line"><span class="type">int</span> <span class="title function_">fputc</span><span class="params">(<span class="type">int</span> ch, FILE *f)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span>((USART1-&gt;SR &amp; (<span class="number">1</span>&lt;&lt;<span class="number">6</span>)) == <span class="number">0</span>);  <span class="comment">//等待DR为空//循环发送,直到发送完毕 </span></span><br><span class="line">    USART1-&gt;DR = (<span class="type">uint8_t</span>) ch;</span><br><span class="line">	<span class="keyword">return</span> ch;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> </span></span><br></pre></td></tr></table></figure>

<h2 id="2-STM32CubeIDE中无法打印处理"><a href="#2-STM32CubeIDE中无法打印处理" class="headerlink" title="2. STM32CubeIDE中无法打印处理"></a><font size=3>2. STM32CubeIDE中无法打印处理</font></h2><p>这种方式主要是针对我们在STM32CubeIDE新建的工程中无法使用printf打印的处理，这里多定义了一些东西，其实这样更通用一些，至少这样可以解决问题。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;sys.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 1</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;usart.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> import(__use_no_semihosting)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> __GNUC__</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PUTCHAR_PROTOTYPE int __io_putchar(int ch)</span></span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="comment">//STM32cubeIDE下</span></span><br><span class="line">PUTCHAR_PROTOTYPE</span><br><span class="line">&#123;</span><br><span class="line">    HAL_UART_Transmit(&amp;huart1 , (<span class="type">uint8_t</span> *)&amp;ch, <span class="number">1</span>,<span class="number">0x200</span>);</span><br><span class="line">    <span class="keyword">return</span> ch;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 标准库需要的支持函数</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">FILE</span> </span></span><br><span class="line"><span class="class">&#123;</span> </span><br><span class="line">	<span class="type">int</span> handle; </span><br><span class="line">&#125;; </span><br><span class="line"></span><br><span class="line">FILE __stdout;</span><br><span class="line">FILE __stdin;</span><br><span class="line"><span class="comment">// 定义_sys_exit()以避免使用半主机模式</span></span><br><span class="line"><span class="type">void</span> _sys_exit(<span class="type">int</span> x) </span><br><span class="line">&#123; </span><br><span class="line">	x = x; </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">// 重定义fputc函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">fputc</span><span class="params">(<span class="type">int</span> ch, FILE *f)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span>((USART1-&gt;SR &amp; (<span class="number">1</span>&lt;&lt;<span class="number">6</span>)) == <span class="number">0</span>);<span class="comment">//等待RDR不为空，表示收到数据</span></span><br><span class="line">	</span><br><span class="line">    USART1-&gt;DR = (<span class="type">uint8_t</span>) ch;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> ch;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> </span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="3-Makefile工程无法打印处理"><a href="#3-Makefile工程无法打印处理" class="headerlink" title="3. Makefile工程无法打印处理"></a><font size=3>3. Makefile工程无法打印处理</font></h2><p>我们使用STM32CubeMX生成的Makefile工程，也是无法使用printf的，但是使用上边的两种方式都不行，makefile使用的交叉编译工具链为</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$ arm-none-eabi-gcc -v</span><br><span class="line">Using built-in specs.</span><br><span class="line">COLLECT_GCC=C:\Program Files (x86)\Arm GNU Toolchain arm-none-eabi\<span class="number">12.2</span> mpacbti-rel1\bin\arm-none-eabi-gcc.exe</span><br><span class="line">COLLECT_LTO_WRAPPER=c:/program\ files\ (x86)/arm\ gnu\ toolchain\ arm-none-eabi/<span class="number">12.2</span>\ mpacbti-rel1/bin/../libexec/gcc/arm-none-eabi/<span class="number">12.2</span><span class="number">.1</span>/lto-wrapper.exe</span><br><span class="line">Target: arm-none-eabi</span><br><span class="line">Configured with: /data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/src/gcc/configure --target=arm-none-eabi --prefix=/data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/build-mingw-arm-none-eabi/install --with-gmp=/data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/build-mingw-arm-none-eabi/host-tools --with-mpfr=/data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/build-mingw-arm-none-eabi/host-tools --with-mpc=/data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/build-mingw-arm-none-eabi/host-tools --with-isl=/data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/build-mingw-arm-none-eabi/host-tools --disable-shared --disable-nls --disable-threads --disable-tls --enable-checking=release --enable-languages=c,c++,<span class="keyword">fortran</span> --with-newlib --with-gnu-as --with-gnu-ld --with-sysroot=/data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/build-mingw-arm-none-eabi/install/arm-none-eabi --with-multilib-<span class="built_in">list</span>=aprofile,rmprofile --with-libiconv-prefix=/data/jenkins/workspace/GNU-toolchain/arm<span class="number">-12</span>-mpacbti/build-mingw-arm-none-eabi/host-tools --host=i686-w64-mingw32 --with-pkgversion=<span class="string">&#x27;Arm GNU Toolchain 12.2.MPACBTI-Rel1 (Build arm-12-mpacbti.34)&#x27;</span> --with-bugurl=https:<span class="comment">//bugs.linaro.org/</span></span><br><span class="line">Thread model: single</span><br><span class="line">Supported LTO compression algorithms: zlib</span><br><span class="line">gcc version <span class="number">12.2</span><span class="number">.1</span> <span class="number">20230214</span> (Arm GNU Toolchain <span class="number">12.2</span>.MPACBTI-Rel1 (Build arm<span class="number">-12</span>-mpacbti<span class="number">.34</span>))</span><br></pre></td></tr></table></figure>

<p>无法打印的处理方式为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./sys.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//加入以下代码,支持printf函数,而不需要选择use MicroLIB	  </span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> __USART_H__</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  重定义 _write 函数 </span></span><br><span class="line"><span class="comment">  * @note   </span></span><br><span class="line"><span class="comment">  * @param  fd 文件描述符</span></span><br><span class="line"><span class="comment">  * @param  ch 要写入的字符串</span></span><br><span class="line"><span class="comment">  * @param  len 要写入的字符串长度</span></span><br><span class="line"><span class="comment">  * @retval len 返回写入字符串的长度</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">int</span> _write(<span class="type">int</span> fd, <span class="type">char</span> *ch, <span class="type">int</span> len)</span><br><span class="line">&#123;</span><br><span class="line">  HAL_UART_Transmit(&amp;huart1, (<span class="type">uint8_t</span> *)ch, len, <span class="number">0xFFFF</span>);</span><br><span class="line">  <span class="keyword">return</span> len;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> </span></span><br></pre></td></tr></table></figure>

<p>注意在要保证这个sys.c文件包含的有串口对应的头文件，我这里是因为包含了main.h，在main.h中包含了usart.h头文件。这个比起另外两种倒是简单了不少。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/f38b79ed.html">LV16-15-串口通信-02-串口接收发送实现</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年06月04日 - 22:07</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/f38b79ed.html" title="LV16-15-串口通信-02-串口接收发送实现">https://sumumm.github.io/post/f38b79ed.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV16-STM32%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV16-STM32开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/e937a4ef.html" rel="prev" title="LV16-16-STM32时钟系统">
                  <i class="fa fa-angle-left"></i> LV16-16-STM32时钟系统
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/e0cf3d57.html" rel="next" title="LV16-15-串口通信-01-串口通信基础">
                  LV16-15-串口通信-01-串口通信基础 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
